#!/bin/bash

# =======================================
# 用户管理系统 API 测试用例
# =======================================
# 说明: 本文件包含所有 API 接口的 curl 测试用例
# 使用方法: bash api_test_cases.sh [api_name]
# 例如: bash api_test_cases.sh user_register
# =======================================

# 服务器基础 URL
BASE_URL="http://localhost:3000/api"

# 认证令牌 (替换为实际获取的 token)
TOKEN=""

# 帮助函数: 显示使用方法
function show_help() {
    echo "用户管理系统 API 测试用例"
    echo "使用方法: bash api_test_cases.sh [api_name]"
    echo ""
    echo "可用的 API 测试用例:"
    echo "  user_register    - 用户注册"
    echo "  user_login       - 用户登录"
    echo "  user_create      - 创建新用户 (需要权限)"
    echo "  user_get_all     - 获取所有用户 (需要权限)"
    echo "  user_get_one     - 获取单个用户 (需要权限)"
    echo "  user_update      - 更新用户 (需要权限)"
    echo "  user_delete      - 删除用户 (需要权限)"
    echo "  user_get_perms   - 获取用户权限 (需要权限)"
    echo "  user_add_perm    - 添加权限给用户 (需要权限)"
    echo "  user_remove_perm - 移除用户的权限 (需要权限)"
    echo "  perm_get_all     - 获取所有权限 (需要权限)"
    echo "  perm_get_one     - 获取单个权限 (需要权限)"
    echo "  perm_create      - 创建新权限 (需要权限)"
    echo "  perm_update      - 更新权限 (需要权限)"
    echo "  perm_delete      - 删除权限 (需要权限)"
    echo "  club_get_all     - 获取所有社团"
    echo "  club_get_by_name - 根据名称获取社团"
    echo "  club_get_one     - 根据ID获取社团"
    echo "  club_create      - 创建社团 (需要权限)"
    echo "  club_update      - 更新社团 (需要权限)"
    echo "  club_delete      - 删除社团 (需要权限)"
    echo "  club_reg_seckill - 社团秒杀报名"
    echo "  club_reg_get_all - 获取所有社团报名记录 (需要权限)"
    echo "  club_reg_get_one - 根据ID获取社团报名记录 (需要权限)"
    echo "  club_reg_create  - 创建社团报名记录 (需要权限)"
    echo "  club_reg_update  - 更新社团报名记录 (需要权限)"
    echo "  club_reg_delete  - 删除社团报名记录 (需要权限)"
    echo "  class_get_all    - 获取所有新生分班表"
    echo "  class_search     - 模糊查询新生分班表"
    echo "  class_get_one    - 根据ID获取新生分班表"
    echo "  class_create     - 新增新生分班表 (需要权限)"
    echo "  class_update     - 更新新生分班表 (需要权限)"
    echo "  class_delete     - 删除新生分班表 (需要权限)"
    echo "  student_get_all  - 获取所有学生 (需要权限)"
    echo "  student_get_one  - 根据ID获取学生 (需要权限)"
    echo "  student_create   - 创建学生 (需要权限)"
    echo "  student_update   - 更新学生 (需要权限)"
    echo "  student_delete   - 删除学生 (需要权限)"
    echo "  help             - 显示帮助信息"
}

# 用户 API 测试用例

# 用户注册
function user_register() {
    echo "测试用户注册 API..."
    curl -X POST "${BASE_URL}/users/register" \
         -H "Content-Type: application/json" \
         -d '{"username":"testuser","password":"testpassword","email":"test@example.com"}'
}

# 用户登录
function user_login() {
    echo "测试用户登录 API..."
    curl -X POST "${BASE_URL}/users/login" \
         -H "Content-Type: application/json" \
         -d '{"username":"testuser","password":"testpassword"}'
}

# 创建新用户 (需要权限)
function user_create() {
    echo "测试创建新用户 API..."
    curl -X POST "${BASE_URL}/users" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"username":"newuser","password":"newpassword","email":"newuser@example.com"}'
}

# 获取所有用户 (需要权限)
function user_get_all() {
    echo "测试获取所有用户 API..."
    curl -X GET "${BASE_URL}/users" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 获取单个用户 (需要权限)
function user_get_one() {
    echo "测试获取单个用户 API..."
    local user_id=${1:-1}
    curl -X GET "${BASE_URL}/users/${user_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 更新用户 (需要权限)
function user_update() {
    echo "测试更新用户 API..."
    local user_id=${1:-1}
    curl -X PUT "${BASE_URL}/users/${user_id}" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"email":"updated@example.com"}'
}

# 删除用户 (需要权限)
function user_delete() {
    echo "测试删除用户 API..."
    local user_id=${1:-1}
    curl -X DELETE "${BASE_URL}/users/${user_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 获取用户权限 (需要权限)
function user_get_perms() {
    echo "测试获取用户权限 API..."
    local user_id=${1:-1}
    curl -X GET "${BASE_URL}/users/permissions/${user_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 添加权限给用户 (需要权限)
function user_add_perm() {
    echo "测试添加权限给用户 API..."
    curl -X POST "${BASE_URL}/users/permissions" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"user_id":1,"permission_id":1}'
}

# 移除用户的权限 (需要权限)
function user_remove_perm() {
    echo "测试移除用户的权限 API..."
    curl -X POST "${BASE_URL}/users/permissions_delete" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"user_id":1,"permission_id":1}'
}

# 权限 API 测试用例

# 获取所有权限 (需要权限)
function perm_get_all() {
    echo "测试获取所有权限 API..."
    curl -X GET "${BASE_URL}/permissions" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 获取单个权限 (需要权限)
function perm_get_one() {
    echo "测试获取单个权限 API..."
    local perm_id=${1:-1}
    curl -X GET "${BASE_URL}/permissions/${perm_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 创建新权限 (需要权限)
function perm_create() {
    echo "测试创建新权限 API..."
    curl -X POST "${BASE_URL}/permissions" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"name":"new_permission","description":"New permission description"}'
}

# 更新权限 (需要权限)
function perm_update() {
    echo "测试更新权限 API..."
    local perm_id=${1:-1}
    curl -X PUT "${BASE_URL}/permissions/${perm_id}" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"description":"Updated permission description"}'
}

# 删除权限 (需要权限)
function perm_delete() {
    echo "测试删除权限 API..."
    local perm_id=${1:-1}
    curl -X DELETE "${BASE_URL}/permissions/${perm_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 社团 API 测试用例

# 获取所有社团
function club_get_all() {
    echo "测试获取所有社团 API..."
    curl -X GET "${BASE_URL}/clubs"
}

# 根据名称获取社团
function club_get_by_name() {
    echo "测试根据名称获取社团 API..."
    local club_name=${1:-"音乐社"}
    curl -X GET "${BASE_URL}/clubs/name?name=${club_name}"
}

# 根据ID获取社团
function club_get_one() {
    echo "测试根据ID获取社团 API..."
    local club_id=${1:-1}
    curl -X GET "${BASE_URL}/clubs/${club_id}"
}

# 创建社团 (需要权限)
function club_create() {
    echo "测试创建社团 API..."
    curl -X POST "${BASE_URL}/clubs" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"club_name":"新社团","location":"教学楼A区3楼","member_count":0,"remarks":"新建社团"}'
}

# 更新社团 (需要权限)
function club_update() {
    echo "测试更新社团 API..."
    local club_id=${1:-1}
    curl -X PUT "${BASE_URL}/clubs/${club_id}" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"location":"教学楼B区2楼","remarks":"更新后的社团信息"}'
}

# 删除社团 (需要权限)
function club_delete() {
    echo "测试删除社团 API..."
    local club_id=${1:-1}
    curl -X DELETE "${BASE_URL}/clubs/${club_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 社团报名 API 测试用例

# 社团秒杀报名
function club_reg_seckill() {
    echo "测试社团秒杀报名 API..."
    curl -X POST "${BASE_URL}/club-registrations/seckill" \
         -H "Content-Type: application/json" \
         -d '{"student_name":"张三","club_name":"音乐社","student_id":"2023001"}'
}

# 获取所有社团报名记录 (需要权限)
function club_reg_get_all() {
    echo "测试获取所有社团报名记录 API..."
    curl -X GET "${BASE_URL}/club-registrations" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 根据ID获取社团报名记录 (需要权限)
function club_reg_get_one() {
    echo "测试根据ID获取社团报名记录 API..."
    local reg_id=${1:-1}
    curl -X GET "${BASE_URL}/club-registrations/${reg_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 创建社团报名记录 (需要权限)
function club_reg_create() {
    echo "测试创建社团报名记录 API..."
    curl -X POST "${BASE_URL}/club-registrations" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"student_id":"2023002","club_id":1,"registration_time":"2025-08-14T10:00:00Z"}'
}

# 更新社团报名记录 (需要权限)
function club_reg_update() {
    echo "测试更新社团报名记录 API..."
    local reg_id=${1:-1}
    curl -X PUT "${BASE_URL}/club-registrations/${reg_id}" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"registration_time":"2025-08-14T11:00:00Z"}'
}

# 删除社团报名记录 (需要权限)
function club_reg_delete() {
    echo "测试删除社团报名记录 API..."
    local reg_id=${1:-1}
    curl -X DELETE "${BASE_URL}/club-registrations/${reg_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 新生分班 API 测试用例

# 获取所有新生分班表
function class_get_all() {
    echo "测试获取所有新生分班表 API..."
    curl -X GET "${BASE_URL}/new-student-classes"
}

# 模糊查询新生分班表
function class_search() {
    echo "测试模糊查询新生分班表 API..."
    local keyword=${1:-"一班"}
    curl -X GET "${BASE_URL}/new-student-classes/search?keyword=${keyword}"
}

# 根据ID获取新生分班表
function class_get_one() {
    echo "测试根据ID获取新生分班表 API..."
    local class_id=${1:-1}
    curl -X GET "${BASE_URL}/new-student-classes/${class_id}"
}

# 新增新生分班表 (需要权限)
function class_create() {
    echo "测试新增新生分班表 API..."
    curl -X POST "${BASE_URL}/new-student-classes" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"class_name":"2025级新生一班","adviser":"王老师","student_count":45}'
}

# 更新新生分班表 (需要权限)
function class_update() {
    echo "测试更新新生分班表 API..."
    local class_id=${1:-1}
    curl -X PUT "${BASE_URL}/new-student-classes/${class_id}" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"student_count":46}'
}

# 删除新生分班表 (需要权限)
function class_delete() {
    echo "测试删除新生分班表 API..."
    local class_id=${1:-1}
    curl -X DELETE "${BASE_URL}/new-student-classes/${class_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 学生 API 测试用例

# 获取所有学生 (需要权限)
function student_get_all() {
    echo "测试获取所有学生 API..."
    curl -X GET "${BASE_URL}/students" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 根据ID获取学生 (需要权限)
function student_get_one() {
    echo "测试根据ID获取学生 API..."
    local student_id=${1:-1}
    curl -X GET "${BASE_URL}/students/${student_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 创建学生 (需要权限)
function student_create() {
    echo "测试创建学生 API..."
    curl -X POST "${BASE_URL}/students" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"student_name":"李四","student_id":"2023003","class_id":1,"gender":"男","age":18}'
}

# 更新学生 (需要权限)
function student_update() {
    echo "测试更新学生 API..."
    local student_id=${1:-1}
    curl -X PUT "${BASE_URL}/students/${student_id}" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer ${TOKEN}" \
         -d '{"age":19}'
}

# 删除学生 (需要权限)
function student_delete() {
    echo "测试删除学生 API..."
    local student_id=${1:-1}
    curl -X DELETE "${BASE_URL}/students/${student_id}" \
         -H "Authorization: Bearer ${TOKEN}"
}

# 执行测试用例
if [ $# -eq 0 ]; then
    show_help
else
    case "$1" in
        user_register) user_register ;;
        user_login) user_login ;;
        user_create) user_create ;;
        user_get_all) user_get_all ;;
        user_get_one) user_get_one ${2} ;;
        user_update) user_update ${2} ;;
        user_delete) user_delete ${2} ;;
        user_get_perms) user_get_perms ${2} ;;
        user_add_perm) user_add_perm ;;
        user_remove_perm) user_remove_perm ;;
        perm_get_all) perm_get_all ;;
        perm_get_one) perm_get_one ${2} ;;
        perm_create) perm_create ;;
        perm_update) perm_update ${2} ;;
        perm_delete) perm_delete ${2} ;;
        club_get_all) club_get_all ;;
        club_get_by_name) club_get_by_name "${2}" ;;
        club_get_one) club_get_one ${2} ;;
        club_create) club_create ;;
        club_update) club_update ${2} ;;
        club_delete) club_delete ${2} ;;
        club_reg_seckill) club_reg_seckill ;;
        club_reg_get_all) club_reg_get_all ;;
        club_reg_get_one) club_reg_get_one ${2} ;;
        club_reg_create) club_reg_create ;;
        club_reg_update) club_reg_update ${2} ;;
        club_reg_delete) club_reg_delete ${2} ;;
        class_get_all) class_get_all ;;
        class_search) class_search "${2}" ;;
        class_get_one) class_get_one ${2} ;;
        class_create) class_create ;;
        class_update) class_update ${2} ;;
        class_delete) class_delete ${2} ;;
        student_get_all) student_get_all ;;
        student_get_one) student_get_one ${2} ;;
        student_create) student_create ;;
        student_update) student_update ${2} ;;
        student_delete) student_delete ${2} ;;
        help) show_help ;;
        *) echo "未知的测试用例: $1" ; show_help ;;
    esac
fi